//
//  HHYWebUtil.m
//  大股票
//
//  Created by apple on 2019/8/15.
//  Copyright © 2019 apple. All rights reserved.
//

#import "HHYWebUtil.h"

#import <AFNetworking/AFNetworking.h>


#define kStockListDataPath @"http://web.juhe.cn:8080/finance/stock/shall"

@implementation HHYWebUtil
+ (void)requestStockListDataWithParameter:(NSDictionary *)para successCallback:(void (^)(NSArray * _Nonnull))successCallback failedCallback:(void (^)(void))failedCallback {
    //1.创建AFHTTPSessionManager管理者
    //AFHTTPSessionManager内部是基于NSURLSession实现的
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //采用JSON的方式来解析数据
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/xml", @"text/plain", nil];
    //注意：responseObject:请求成功返回的响应结果（AFN内部已经把响应体转换为OC对象，通常是字典或数组）
    __weak typeof(self)wSelf = self;
    [manager GET:kStockListDataPath parameters:para progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"reason"] isEqualToString:@"SUCCESSED!"]) {
            NSArray *result = responseObject[@"result"][@"data"];
            successCallback(result);
        } else {
            failedCallback();
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failedCallback();
    }];
}

//+ (NSString *)convertPathWithBasePath:(NSString *)path para:(NSDictionary *)para {
//
//    NSString *basePath = path;
//    __block NSString *subPath;
//    [para enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
//        if (!subPath) {
//            subPath = [NSString stringWithFormat:@"?%@=%@", key, obj];
//        } else {
//            subPath = [NSString stringWithFormat:@"&%@=%@", key, obj];
//        }
//    }];
//
//    basePath = [basePath stringByAppendingString:subPath];
//
//    return basePath;
//}

@end
